﻿Ext.define('Html5App.view.information.InformationImage', {
	alternateClassName : 'informationImage',
	extend : 'Ext.Container',
	xtype : 'informationImage',
	id : 'informationImage',
	requires : ['Ext.ux.touch.ImageGridList.view.Panel','Html5App.store.FileImageStore','Ext.field.Hidden','ux.ImageCarousel'],
	config : {
		height : '100%',
		xtype : 'container',
		style : 'background-color: #FFFFFF;',
		items : [{
			flex: 1,
			height : '100%',
			xtype: 'image-grid-list-panel',
			id: 'image_grid_list_panel'
		},{
	        xtype: 'hiddenfield', 
	        id: 'imgsHidden',
	        height : '0px'
	    }],
        listeners: {
        	activate: 'onInitThisPanel'
        }
	},
	onInitThisPanel: function(){
        Ext.returnArray = [];
        var me = this;
        config.database.transaction(
			function (tx) {
                tx.executeSql("select * from LocalFile where filetype = '"+config.file_type+"'",
                	[],
                	function (tx, r) {
                		if(Number(r.rows.length) > 0){
                			for (var i = 0; i < r.rows.length; i++) {
                				var filepath = cordova.file.externalDataDirectory+r.rows.item(i)['filepath'];
                				Ext.returnArray.push(filepath);
                			}
                			me.setImageData();
                		}else{
                			util.showMessageMask('無圖片資訊文件','warn-max');
                		}
                	},
                	function (tx, error) {}
                );
			}
		);
    },
    setImageData: function(){
        var me = this,
        	gallery = me.down('image-grid-list-panel'),
        	store = me.getImageData();
        gallery.renderImages(store);
    },
    getImageData: function(){
        var me = this,
            store = Ext.create('Html5App.store.FileImageStore'),
            urls = me.getUrls(),
            model;
        for(var i = 0, length = urls.length; i < length; i++){
            model = Ext.create(Html5App.model.FileImage);
            model.set('url', urls[i]);
            model.set('indexStr', i);
            store.add(model);
        }
        Ext.getCmp("imgsHidden").setData(urls);
        return store;
    },
    getUrls: function(){
        return Ext.returnArray;
    }
});